--及锋而试
local jifengershi = fk.CreateSkill {
    name = "mcdh_jifengershi"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

jifengershi:addEffect('viewas',{
    pattern = ".",
    interaction = function(self,player)
        local names, all_names = {} , {}
        local all_cards = Fk:getAllCardIds()
        table.sort(all_cards,function (a, b)
            local carda = Fk:getCardById(a)
            local cardb = Fk:getCardById(b)
            if carda.type == cardb.type then
                return a<b
            end
            return carda.type < cardb.type
        end)
        for _, id in ipairs(all_cards) do
            local card = Fk:getCardById(id)
            if (card.type == Card.TypeBasic or card.trueName == "duel") and not card.is_derived then
                table.insertIfNeed(all_names, card.name)
                if ((Fk.currentResponsePattern == nil and player:canUse(card)) or
                        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
                    table.insertIfNeed(names, card.name)
                end
            end
        end
        return U.CardNameBox {choices = names, all_choices = all_names}
    end,
    card_filter = Util.FalseFunc,
    view_as = function(self, player,cards)
        local card = Fk:cloneCard(self.interaction.data)
        card:addSubcards(player:getCardIds(Player.Hand))
        card.skillName = self.name
        return card
    end,
    before_use = function (self, player, use)
        player.room:handleAddLoseSkills(player,"-mcdh_jifengershi")
    end,
    enabled_at_play = function(self, player)
        return #player:getCardIds(Player.Hand) == 1
    end,
    enabled_at_response = function (self, player, response)
        if not response and #player:getCardIds(Player.Hand) == 1 and Fk.currentResponsePattern then
            for _, id in ipairs(Fk:getAllCardIds()) do
                local card = Fk:getCardById(id)
                if (card.type == Card.TypeBasic or card.trueName == "duel") and not card.is_derived and
                        Exppattern:Parse(Fk.currentResponsePattern):match(card) then
                    return true
                end
            end
        end
    end
})

jifengershi:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(self) then return end
        if not player:isKongcheng() then return end
        for _, move in ipairs(data) do
            if move.from == player.id then
                for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.PlayerHand then
                        return true
                    end
                end
            end
        end
    end,
    on_use = function(self, event, target, player, data)
        player.room:handleAddLoseSkills(player,"-mcdh_jifengershi")
        player:drawCards(2, self.name)
    end,
})

Fk:loadTranslationTable {
    ["mcdh_jifengershi"] = "锋试",
    [":mcdh_jifengershi"] = "『及锋而试』<br>你可以失去本技能，将最后一张手牌当【决斗】或任意基本牌使用。当你失去手牌后，若你没有手牌，你可以失去本技能并摸两张牌。",
}

return jifengershi